package onlinebanking.client.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import onlinebanking.client.SimpleClient;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ConfirmTransactionPanel extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel confirmTransactionHeader;
	private JLabel pendingTransactionsLabel;
	private JScrollPane PendingTransactionsScrollPane;
	private JFrame jFrame1;
	private JDialog ConfirmDialog;
	private JButton ConfirmButton;
	private JTable PendingTransactionsTable;
	
	DefaultTableModel PendingTransactionsTableModel;
	TableCellRenderer PendingTransactionsTableCellRenderer;

	private ClientPanel parent;
	
	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new ConfirmTransactionPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public ConfirmTransactionPanel() {
		super();
		initGUI();
	}
	
	public ConfirmTransactionPanel(ClientPanel clientPanel) {
		super();
		this.parent = clientPanel;
		initGUI();
		sendCommand();
	}

	public void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(582, 444));
			this.setLayout(null);
			{
				confirmTransactionHeader = new JLabel();
				this.add(confirmTransactionHeader);
				confirmTransactionHeader.setText("CONFIRM TRANSACTION");
				confirmTransactionHeader.setLayout(null);
				confirmTransactionHeader.setBounds(12, 12, 199, 16);
			}
			{
				pendingTransactionsLabel = new JLabel();
				this.add(pendingTransactionsLabel);
				pendingTransactionsLabel.setText("Pending Transactions");
				pendingTransactionsLabel.setBounds(18, 40, 162, 24);
			}
			{
				PendingTransactionsScrollPane = new JScrollPane();
				this.add(PendingTransactionsScrollPane);
				PendingTransactionsScrollPane.setBounds(12, 70, 564, 202);
				{
					PendingTransactionsTableModel = 
						new DefaultTableModel(){ @Override
					    public boolean isCellEditable(int row, int column) {
					       //all cells false
					       return false;
					    }
						
					};	
//						(new String[] {"ID", "Account", "Type", "Ammount","Owner", "Expiry" },0);
					PendingTransactionsTableModel.addColumn("ID");
					PendingTransactionsTableModel.addColumn("Account");
					PendingTransactionsTableModel.addColumn("Type");
					PendingTransactionsTableModel.addColumn("Ammount");
					PendingTransactionsTableModel.addColumn("Owner");
					PendingTransactionsTableModel.addColumn("Expiry");
					PendingTransactionsTableModel.isCellEditable(0, 1);
					
					PendingTransactionsTable = new JTable(PendingTransactionsTableModel);
					
					PendingTransactionsScrollPane.setViewportView(PendingTransactionsTable);
					PendingTransactionsTable.setModel(PendingTransactionsTableModel);
					
					
					PendingTransactionsTable.setBounds(27, 188, 451, 41);
					
				}
			}
			{
				ConfirmButton = new JButton();
				this.add(ConfirmButton);
				ConfirmButton.setText("Confirm Transaction");
				ConfirmButton.setBounds(396, 284, 167, 31);
				ConfirmButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						ConfirmButtonActionPerformed(evt);
					}
				});
			}
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void ConfirmButtonActionPerformed(ActionEvent evt) {
		System.out.println("ConfirmButton");
		//TODO add your code for ConfirmButton.actionPerformed
		int selectedrow = PendingTransactionsTable.getSelectedRow();
		if(selectedrow >= 0){
			int transactionid = Integer.parseInt((String)(PendingTransactionsTable.getModel().getValueAt(selectedrow, 0)));
						
			//switch view and send command to server
			this.parent.switchToConfirmTransactionDetail(transactionid);
		//or the panel will send the transactionid
		//	this.parent.getSimpleClient().viewPendingTransactionDetails(transactionid);
			
			
			
		}else{
			JOptionPane.showMessageDialog(null, "Please Select a transaction to confirm");
		}
	}
	public void sendCommand(){
		System.out.println("Sending Command");//TODO:
		parent.getSimpleClient().viewPendingTransactions();
	}
	
	public JTable getPendingTransactionsTable() {
		return PendingTransactionsTable;
	}
	
	public DefaultTableModel getPendingTransactionsTableModel(){
		return PendingTransactionsTableModel;
	}
	
	public void addRow(Object[] rowdata){
		PendingTransactionsTableModel.addRow(rowdata);
	}
	
	public void addRow(String transactionid,String accountno, String transactiontype,
			String ammount, String initiatedby, String expiry){
		this.addRow(new Object[]{transactionid,accountno,transactiontype,ammount,initiatedby,expiry});
//		JTextArea textArea = (JTextArea)PendingTransactionsTable.getEditorComponent();
//		textArea.setEditable(false);  
//		PendingTransactionsTable.setCellSelectionEnabled(false);
//		PendingTransactionsTable.setFocusable(true);
	}
	
	
	public void removeRow(int row){
		PendingTransactionsTableModel.removeRow(row);
		
	}
	
	

}
